package ex.mlist;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;

import Database.DBHelper;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public
class EditTime extends Activity {
	
	
	private TimePicker time1,time2;
    private Button button_ringtone;
    int profile_id;
    private Button button_set;
    private TextView days;
    private String ringtone_src;
    static int number;
    int size;
     DBHelper d;
     //String ringtone_src;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		profile_id=1;
		d=new DBHelper(getApplicationContext());
		d.openDB();
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.time);
		time1=(TimePicker) findViewById(R.id.timePicker1);
		time2=(TimePicker) findViewById(R.id.timePicker2);
		
		button_ringtone=(Button) findViewById(R.id.ringtone);
		
		button_set=(Button) findViewById(R.id.set);
		if(getIntent().hasExtra("profile_id")){
			profile_id=getIntent().getExtras().getInt("profile_id",1);
			}
		
		setValuesForObjects();
		
		
		
		button_set.setOnClickListener(new OnClickListener() 
		{
			
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				
				setAlarm();
				
				d.editTime(number,time1.getCurrentHour() , time1.getCurrentMinute(),time2.getCurrentHour(),time2.getCurrentMinute(),ringtone_src
						, profile_id);
				d.close();
				SharedPreferences settings=getSharedPreferences("prefs", 0);
				SharedPreferences.Editor editor=settings.edit();
				editor.putString("number", number+"");
				editor.commit();
				Intent intent = new Intent(EditTime.this,ProfileActivity.class);
				intent.putExtra("profile_id", profile_id);
				startActivity(intent);
				
			}
		});
		
		
		button_ringtone.setOnClickListener(new OnClickListener() 
		{
			
			public void onClick(View v)
			{
				Intent intent=new Intent(EditTime.this,RlistActivity.class);
				
				intent.putExtra("strt_hrs", time1.getCurrentHour());
				
				intent.putExtra("strt_min", time1.getCurrentMinute());
				intent.putExtra("end_hrs", time2.getCurrentHour());
				intent.putExtra("end_min", time2.getCurrentMinute());
				startActivity(intent);
				
				
			}
		});
	}
		
		
		
	
	
	
	

		private void setValuesForObjects() {
			
			if(getIntent()!=null && getIntent().getExtras()!=null && getIntent().getExtras().getString("ringtone_src")!=null)
			{
				ringtone_src=getIntent().getExtras().getString("ringtone_src");
				time1.setCurrentHour(getIntent().getExtras().getInt("strt_hrs",time1.getCurrentHour()));
				time1.setCurrentMinute(getIntent().getExtras().getInt("strt_min",time1.getCurrentMinute()));
				time2.setCurrentHour(getIntent().getExtras().getInt("end_hrs",time2.getCurrentHour()));
				time2.setCurrentMinute(getIntent().getExtras().getInt("end_min",time2.getCurrentMinute()));
				size=getIntent().getExtras().getInt("size");
			}
			else
			{
				SharedPreferences tones = getSharedPreferences("MYLIST", 0);
				ringtone_src=tones.getString("0", "21 Guns.mp3");
				
				
			}
			
			
			
			
			SharedPreferences settings=getSharedPreferences("prefs", 0);
			number=Integer.parseInt(settings.getString("number", "1"));
			number++;
			if(getIntent()!=null && getIntent().getExtras()!=null)
			{
				time1.setCurrentHour(getIntent().getExtras().getInt("strt_hrs",time1.getCurrentHour()));
				time1.setCurrentMinute(getIntent().getExtras().getInt("strt_min",time1.getCurrentMinute()));
				time2.setCurrentHour(getIntent().getExtras().getInt("end_hrs",time2.getCurrentHour()));
				time2.setCurrentMinute(getIntent().getExtras().getInt("end_min",time2.getCurrentMinute()));
				size=getIntent().getExtras().getInt("size");
			}
			
			
			
			
			
	}

		protected void setAlarm() {
		
			Time t1=new Time(time1.getCurrentHour(), time1.getCurrentMinute(), 0);
			Time t2=new Time(time2.getCurrentHour(), time2.getCurrentMinute(), 0);
			
			Date d=new Date();
			long hrs=t1.getHours()-d.getHours();
			long min=t1.getMinutes()-d.getMinutes();
			if(hrs<0)
			{
				hrs=0;
				min=0;
			}
			else if(min<0 )
			{
				min=60+min;
				if(hrs!=0)
					hrs=hrs-1;
				
			}
			
			long alarm_time=System.currentTimeMillis()+hrs*3600000+min*60000;
			Intent intent= new Intent(EditTime.this, Schedule_service.class);
			intent.putExtra("rinftone_src", ringtone_src);
			intent.putExtra("number", number);
			intent.putExtra("profile_id",profile_id);
			intent.putExtra("size", size);
			intent.putExtra("end", 0);
			PendingIntent sender = PendingIntent.getBroadcast(this, number, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
			am.set(AlarmManager.RTC_WAKEUP,alarm_time, sender);
			//Toast.makeText(this,"Alarm set in "+hrs+"hours "+min+"minutes"+" "+ringtone_src)+" "+ days.getText(),
					//Toast.LENGTH_LONG).show();
			
			
			
			
			long ehrs=t2.getHours()-d.getHours();
			long emin=t2.getMinutes()-d.getMinutes();
			if(ehrs<0)
			{
				ehrs=0;
				emin=0;
			}
			else if(emin<0 )
			{
				emin=60+emin;
				if(ehrs!=0)
					ehrs=ehrs-1;
				
			}
			
			long alarm_time2=System.currentTimeMillis()+ehrs*3600000+emin*60000;
			Intent myintent= new Intent(EditTime.this, Schedule_service.class);
			myintent.putExtra("rinftone_src", ringtone_src);
			intent.putExtra("number", number);
			myintent.putExtra("end", 1);
			PendingIntent mysender = PendingIntent.getBroadcast(this, number+8, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
			AlarmManager myam = (AlarmManager) getSystemService(ALARM_SERVICE);
			myam.set(AlarmManager.RTC_WAKEUP,alarm_time2, mysender);
			//Toast.makeText(this,"Alarm set in "+hrs+"hours "+min+"minutes"+" "+ringtone_src.getText()+" "+ days.getText(),
					//Toast.LENGTH_LONG).show();
			
			
		
	}
		
		private void commitsharedPreferences() {
			// TODO Auto-generated method stub
			SharedPreferences settings=getSharedPreferences("prefs", 0);
			SharedPreferences.Editor editor=settings.edit();
			editor.putString("number", number+"");
			editor.commit();
		}
	
	
	
}
